<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/9/20
  Time: 14:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
<%
    //这个要设置在传输数据之前
    request.setCharacterEncoding("UTF-8");
%>
<h1 style="color: #3880c2">
    注册页面
</h1>
<h1 style="color: #3880c2">
    <a href="index.jsp" style="color: crimson">返回主页面</a>
</h1>

<%--关键在于给表单中的input标签的id起名，否则js中无法获取--%>

<form onsubmit="return isvalid()" method="post" name="form" <%--action="" 不写默认本页面？--%>>
    <label>
        用户名
        <input name="username" type="text" id="username">
    </label>
    <label>
        密码
        <input name="password" type="password" id="password">
    </label>
    <input type="submit" name="submit"<%--这个name要写一下，不然request找不到--%> value="注册">
</form>

<script>
    //不知道为什么，参照老师的写法不能接收到参数，我参考Bito的Ai的建议很快解决了问题
    function isvalid() {
        // 注意，是ById
        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;
        if (username.trim() === "") {
            alert("输入用户名吧！😊😊😊");
            document.form.username.focus();
            return false; // 阻止提交表单
        }
        if (password.trim() === "") {
            alert("输入密码吧！😅😅😅");
            document.form.username.focus();
            return false;
        }
        return true; // 提交表单
    }
</script>

<%
    if (request.getParameter("submit") != null) {
        String username = request.getParameter("username");
        out.print(username);
        String password = request.getParameter("password");
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8", "root", "123456");
            // 白搭，弄了还是乱码 connection.createStatement().execute("SET NAMES 'utf8'");
            PreparedStatement preparedStatement = connection.prepareStatement("insert into user (username, password) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                out.print("注册成功!");
            } else {
                out.print("注册失败!");
            }
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            out.print(e.getMessage());
            e.printStackTrace();
        }
    }
           /*       获取刚刚增加的数据的ID
                int voteId = 0;
                ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
                //获取自动增加的id号
                if (generatedKeys.next()) {
                    voteId = generatedKeys.getInt(1);
                }
                out.print(voteId);*/
%>


</body>
</html>


